<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <title>Password Reset</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="https://code.getmdl.io/1.2.1/material.indigo-pink.min.css">

    <style media="screen">
      body, #root {
        width: 100%;
        height: 100%;
        margin: 0;
        background: #fff;
      }

      #root form {
        max-width: 300px;
        border: 1px solid lightgrey;
        box-shadow: 0px 10px 24px 2px rgba(0,0,0,0.2);
        margin: 50px auto;
        padding: 15px;
      }

      .legend {
        text-align: center;
        width: 100%;
        font-weight: bold;
      }

      label {
        display: block;
        margin-top: 10px;
        margin-bottom: 3px;
        padding-right: 30px;
      }

      small {
        color: #888;
      }

      input {
        border-radius: 4px;
        margin-top: 3px;
        border: 1px solid lightgrey;
        font-size: 16px;
        width: 100%;
        padding: 14px;
        height: 100%;
        display: inline-block;
      }

      .submit-password-reset {
        border-radius: 4px;
        border: none;
        display: block;
        background-color: #333;
        color: white;
        text-align: center;
        width: 100%;
        padding: 10px;
        margin-top: 10px;
        cursor: pointer;
      }

      .error-console {
        display: none;
        margin-top: 10px;
        border-radius: 4px;
        background-color: pink;
        color: red;
        border: 1px solid red;
        padding: 10px;
      }

      .error-console.active {
        display: block;
      }
    </style>
  </head>
  <body>
    <div id="root">
      <form id="reset-password-form">
        <legend class="legend">Set new password</legend>
        <label for="password">
          New password
          <input type="password" name="password" placeholder="new password" />
          <p><small>Password must be at least 8 characters</small></p>
        </label>

        <label for="confirm-password">
          Confirm password
          <input type="password" name="confirm-password" placeholder="confirm password" />
        </label>
        <button class="submit-password-reset" type="submit">Apply</button>
        <div class="error-console">foo</div>
      </form>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
      $(function () {
        function showError(message) {
          $('.error-console').text(message).addClass('active');
        }

        function handleSubmit (e) {
          e.preventDefault();
          $('.error-console').removeClass('active');

          var password = $('[name="password"]').val();
          var confirm = $('[name="confirm-password"]').val();

          if (password !== confirm || password === '' || password.length < 8) {
            showError('passwords must match and be 8 characters.');
            return false;
          }

          $.ajax({
            url: '<%= BASE_PATH %>api/v1/account/password/reset',
            contentType: 'application/json',
            method: 'PUT',
            data: JSON.stringify({password: password, token: location.hash.replace('#', '')})
          }).then(function (success) {
            location.href = success.redirect;
          }).catch(function (error) {
            showError(error.responseText);
          });
        }

        $('#reset-password-form').on('submit', handleSubmit);
      });
    </script>
  </body>
</html>
